<template>
  <view class="operation-records">
    <!-- 自定义导航栏 -->
    <cu-custom bgColor="bg-gradual-blue" :isBack="true">
      <block slot="content">
        <text class="nav-title">物料操作记录</text>
      </block>
      <block slot="right">
        <view class="nav-right" @click="openFilterModal">
          <text class="cuIcon-filter" :class="{ 'filter-active': hasActiveFilters }"></text>
          <text class="filter-text" :class="{ 'filter-active': hasActiveFilters }">筛选</text>
          <view v-if="hasActiveFilters" class="filter-dot"></view>
        </view>
      </block>
    </cu-custom>

    <!-- Tab切换 -->
    <view class="tab-container">
      <view class="tab-list">
        <view
          class="tab-item"
          :class="{ active: activeTab === 'inbound' }"
          @click="switchTab('inbound')"
        >
          <text class="tab-text">入库记录</text>
        </view>
        <view
          class="tab-item"
          :class="{ active: activeTab === 'outbound' }"
          @click="switchTab('outbound')"
        >
          <text class="tab-text">出库记录</text>
        </view>
      </view>
    </view>

    <!-- 记录列表 -->
    <view class="records-container">
      <!-- 入库记录 -->
      <view v-if="activeTab === 'inbound'" class="record-list">
        <view v-if="inboundLoading" class="loading-container">
          <uni-load-more
            status="loading"
            content-text="{ contentText: { contentdown: '上拉显示更多', contentrefresh: '正在加载...', contentnomore: '没有更多数据了' } }"
          ></uni-load-more>
        </view>
        <view v-else-if="inboundRecords.length === 0" class="empty-container">
          <view class="empty-icon">
            <text class="iconfont icon-empty"></text>
          </view>
          <text class="empty-text">暂无入库操作记录</text>
        </view>
        <view v-else>
          <view
            v-for="record in inboundRecords"
            :key="record.id"
            class="record-item inbound-item"
            @click="viewInboundDetail(record)"
          >
            <view class="record-header">
              <view class="record-title">
                <view class="order-no">{{ record.inboundNo }}</view>
                <view class="record-status" :class="'status-' + record.status">
                  {{ getInboundStatusText(record.status) }}
                </view>
              </view>
              <view class="record-time">{{
                formatTime(record.updateTime)
              }}</view>
            </view>
            <view class="record-content">
              <view class="record-info">
                <!-- <view class="info-item">
                  <text class="info-label">入库类型:</text>
                  <text class="info-value">{{
                    getInboundTypeText(record)
                  }}</text>
                </view> -->
                <view class="info-item" v-if="record.supplierName">
                  <text class="info-label">供应商:</text>
                  <text class="info-value">{{ record.supplierName }}</text>
                </view>
                <view class="info-item" v-if="record.warehouseName">
                  <text class="info-label">仓库:</text>
                  <text class="info-value">{{ record.warehouseName }}</text>
                </view>
              </view>
              <view
                class="record-details"
                v-if="
                  record.inboundDetailList &&
                  record.inboundDetailList.length > 0
                "
              >
                <view class="detail-summary">
                  共 {{ record.inboundDetailList.length }} 种物料
                </view>
                <view class="detail-list">
                  <view
                    v-for="(
                      detail, detailIndex
                    ) in record.inboundDetailList.slice(0, 2)"
                    :key="detailIndex"
                    class="detail-item"
                  >
                    <text class="material-name">{{ detail.materialName }}</text>
                    <text class="material-quantity"
                      >{{ detail.quantity }} {{ detail.unit }}</text
                    >
                  </view>
                  <view
                    v-if="record.inboundDetailList.length > 2"
                    class="more-details"
                  >
                    还有 {{ record.inboundDetailList.length - 2 }} 种物料...
                  </view>
                </view>
              </view>
            </view>

            <!-- 用户操作详情展开面板 -->
            <view
              v-if="isRecordExpanded(record.id)"
              class="user-operations-panel"
            >
              <view class="panel-header">
                <text class="panel-title">操作明细</text>
                <view
                  class="inbound-type-tag"
                  :class="[getTypeClass(record.businessType, 'inbound')]"
                >
                  <text class="type-text">{{
                    getInboundTypeText(record)
                  }}</text>
                </view>
              </view>
              <view class="operations-list">
                <view
                  v-for="(detail, detailIndex) in record.detailList"
                  :key="detailIndex"
                  class="operation-item"
                >
                  <view class="item-header">
                    <view class="material-name">{{ detail.materialName }}</view>
                    <view class="operation-time">{{
                      formatTime(detail.updateTime || detail.createTime)
                    }}</view>
                  </view>

                  <view class="item-content">
                    <view class="material-details">
                      <view class="detail-row" v-if="detail.materialCode">
                        <text class="label">编码</text>
                        <text class="value">{{ detail.materialCode }}</text>
                      </view>
                      <view class="detail-row" v-if="detail.specification">
                        <text class="label">规格</text>
                        <text class="value">{{ detail.specification }}</text>
                      </view>
                      <view class="detail-row" v-if="detail.materialTypeName">
                        <text class="label">类型</text>
                        <text class="value">{{ detail.materialTypeName }}</text>
                      </view>
                    </view>

                    <view class="quantity-section">
                      <view class="quantity-item plan">
                        <text class="qty-label">计划</text>
                        <text class="qty-value">{{ detail.planQuantity }}</text>
                        <text class="qty-unit">{{ detail.unit }}</text>
                      </view>
                      <view class="quantity-item actual">
                        <text class="qty-label">实际</text>
                        <text class="qty-value">{{
                          detail.actualQuantity
                        }}</text>
                        <text class="qty-unit">{{ detail.unit }}</text>
                      </view>
                    </view>
                  </view>

                  <view
                    class="item-footer"
                    v-if="detail.actualWarehouseName || detail.remark"
                  >
                    <view
                      class="location-row"
                      v-if="detail.actualWarehouseName"
                    >
                      <text class="location-icon">📍</text>
                      <text class="location-text"
                        >{{ detail.actualWarehouseName }} -
                        {{ detail.actualLocationName }}</text
                      >
                    </view>
                    <view class="remark-row" v-if="detail.remark">
                      <text class="remark-icon">💬</text>
                      <text class="remark-text">{{ detail.remark }}</text>
                    </view>
                  </view>
                </view>
                <view
                  v-if="!record.detailList || record.detailList.length === 0"
                  class="no-operations"
                >
                  <text>暂无操作明细</text>
                </view>
              </view>
            </view>

            <!-- 展开/收起按钮 -->
            <view
              class="expand-button"
              @click.stop="toggleRecordExpand(record.id)"
            >
              <text class="expand-text">{{
                isRecordExpanded(record.id) ? "收起详情" : "查看我的操作"
              }}</text>
              <text
                class="expand-icon"
                :class="{ expanded: isRecordExpanded(record.id) }"
                >▼</text
              >
            </view>
          </view>
        </view>
      </view>

      <!-- 出库记录 -->
      <view v-if="activeTab === 'outbound'" class="record-list">
        <view v-if="outboundLoading" class="loading-container">
          <uni-load-more
            status="loading"
            content-text="{ contentText: { contentdown: '上拉显示更多', contentrefresh: '正在加载...', contentnomore: '没有更多数据了' } }"
          ></uni-load-more>
        </view>
        <view v-else-if="outboundRecords.length === 0" class="empty-container">
          <view class="empty-icon">
            <text class="iconfont icon-empty"></text>
          </view>
          <text class="empty-text">暂无出库操作记录</text>
        </view>
        <view v-else>
          <view
            v-for="record in outboundRecords"
            :key="record.id"
            class="record-item outbound-item"
            @click="viewOutboundDetail(record)"
          >
            <view class="record-header">
              <view class="record-title">
                <view class="order-no">{{ record.outboundNo }}</view>
                <view class="record-status" :class="'status-' + record.status">
                  {{ getOutboundStatusText(record.status) }}
                </view>
              </view>
              <view class="record-time">{{
                formatTime(record.updateTime)
              }}</view>
            </view>
            <view class="record-content">
              <view class="record-info">
                <!-- <view class="info-item">
                  <text class="info-label">出库类型:</text>
                  <text class="info-value">{{
                    getOutboundTypeText(record.outboundType)
                  }}</text>
                </view> -->
              </view>
              <view
                class="record-details"
                v-if="
                  record.outboundDetailList &&
                  record.outboundDetailList.length > 0
                "
              >
                <view class="detail-summary">
                  共 {{ record.outboundDetailList.length }} 种物料
                </view>
                <view class="detail-list">
                  <view
                    v-for="(
                      detail, detailIndex
                    ) in record.outboundDetailList.slice(0, 2)"
                    :key="detailIndex"
                    class="detail-item"
                  >
                    <text class="material-name">{{ detail.materialName }}</text>
                    <text class="material-quantity"
                      >{{ detail.quantity }} {{ detail.unit }}</text
                    >
                  </view>
                  <view
                    v-if="record.outboundDetailList.length > 2"
                    class="more-details"
                  >
                    还有 {{ record.outboundDetailList.length - 2 }} 种物料...
                  </view>
                </view>
              </view>
            </view>

            <!-- 用户操作详情展开面板 -->
            <view
              v-if="isRecordExpanded(record.id)"
              class="user-operations-panel"
            >
              <view class="panel-header">
                <text class="panel-title">操作明细</text>
                <view
                  class="outbound-type-tag"
                  :class="[getTypeClass(record.outboundType, 'outbound')]"
                >
                  <text class="type-text">{{
                    getOutboundTypeText(record.outboundType)
                  }}</text>
                </view>
              </view>
              <view class="operations-list">
                <view
                  v-for="(plan, planIndex) in record.allocationPlanList"
                  :key="planIndex"
                  class="operation-item"
                >
                  <view class="item-header">
                    <view class="material-name">{{ plan.materialName }}</view>
                    <view class="operation-time">{{
                      formatTime(
                        plan.outboundTime || plan.updateTime || plan.createTime
                      )
                    }}</view>
                  </view>

                  <view class="item-content">
                    <view class="material-details">
                      <view class="detail-row" v-if="plan.materialCode">
                        <text class="label">编码</text>
                        <text class="value">{{ plan.materialCode }}</text>
                      </view>
                      <view class="detail-row" v-if="plan.specification">
                        <text class="label">规格</text>
                        <text class="value">{{ plan.specification }}</text>
                      </view>
                      <view class="detail-row" v-if="plan.color">
                        <text class="label">颜色</text>
                        <text class="value">{{ plan.color }}</text>
                      </view>
                      <view class="detail-row" v-if="plan.batchNo">
                        <text class="label">批次</text>
                        <text class="value">{{ plan.batchNo }}</text>
                      </view>
                      <view class="detail-row" v-if="plan.status !== undefined">
                        <text class="label">状态</text>
                        <text class="value">{{
                          getAllocationPlanStatusText(plan.status)
                        }}</text>
                      </view>
                    </view>

                    <view class="quantity-section">
                      <view class="quantity-item plan">
                        <text class="qty-label">需求</text>
                        <text class="qty-value">{{
                          plan.requiredQuantity
                        }}</text>
                        <text class="qty-unit">{{ plan.unit }}</text>
                      </view>
                      <view class="quantity-item allocated">
                        <text class="qty-label">分配</text>
                        <text class="qty-value">{{
                          plan.allocationQuantity
                        }}</text>
                        <text class="qty-unit">{{ plan.unit }}</text>
                      </view>
                      <view class="quantity-item actual">
                        <text class="qty-label">实际</text>
                        <text class="qty-value">{{
                          plan.actualQuantity || 0
                        }}</text>
                        <text class="qty-unit">{{ plan.unit }}</text>
                      </view>
                    </view>
                  </view>

                  <view
                    class="item-footer"
                    v-if="plan.warehouseName || plan.remark"
                  >
                    <view class="location-row" v-if="plan.warehouseName">
                      <text class="location-icon">📍</text>
                      <text class="location-text"
                        >{{ plan.warehouseName }} -
                        {{ plan.locationName }}</text
                      >
                    </view>
                    <view class="remark-row" v-if="plan.remark">
                      <text class="remark-icon">💬</text>
                      <text class="remark-text">{{ plan.remark }}</text>
                    </view>
                  </view>
                </view>
                <view
                  v-if="
                    !record.allocationPlanList ||
                    record.allocationPlanList.length === 0
                  "
                  class="no-operations"
                >
                  <text>暂无操作明细</text>
                </view>
              </view>
            </view>

            <!-- 展开/收起按钮 -->
            <view
              class="expand-button"
              @click.stop="toggleRecordExpand(record.id)"
            >
              <text class="expand-text">{{
                isRecordExpanded(record.id) ? "收起详情" : "查看我的操作"
              }}</text>
              <text
                class="expand-icon"
                :class="{ expanded: isRecordExpanded(record.id) }"
                >▼</text
              >
            </view>
          </view>
        </view>
      </view>
    </view>

    <!-- 加载更多 -->
    <view class="load-more-container">
      <uni-load-more
        :status="loadMoreStatus"
        :content-text="{
          contentdown: '上拉加载更多',
          contentrefresh: '加载中...',
          contentnomore: '没有更多数据了',
        }"
        @clickLoadMore="loadMore"
      ></uni-load-more>
    </view>

    <!-- 筛选弹窗 -->
    <view
      class="filter-modal"
      v-if="showFilterModal"
      @click="showFilterModal = false"
    >
      <view class="filter-content" @click.stop>
        <view class="filter-header">
          <text class="filter-title">筛选条件</text>
          <view class="close-btn" @click="showFilterModal = false">
            <text class="cuIcon-close"></text>
          </view>
        </view>

        <scroll-view class="filter-body" scroll-y="true">
          <!-- 入库记录筛选条件 -->
          <view v-if="activeTab === 'inbound'">
            <!-- 入库单编号 -->
            <view class="filter-section">
              <view class="group-title">入库单编号</view>
              <input
                class="input-field"
                placeholder="请输入入库单编号"
                v-model="filterForm.inboundNo"
              />
            </view>

            <!-- 入库状态 -->
            <view class="filter-section">
              <view class="group-title">入库状态</view>
              <view class="checkbox-group">
                <view
                  v-for="status in inboundStatuses"
                  :key="status.value"
                  class="checkbox-item"
                  :class="{ checked: filterForm.inboundStatus.includes(status.value) }"
                  @click="toggleStatus(status.value, 'inbound')"
                >
                  <text class="checkbox-text">{{ status.label }}</text>
                </view>
              </view>
            </view>

            <!-- 入库类型 -->
            <view class="filter-section">
              <view class="group-title">入库类型</view>
              <view class="checkbox-group">
                <view
                  v-for="type in inboundTypes"
                  :key="type.value"
                  class="checkbox-item"
                  :class="{ checked: filterForm.inboundType.includes(type.value) }"
                  @click="toggleType(type.value, 'inbound')"
                >
                  <text class="checkbox-text">{{ type.label }}</text>
                </view>
              </view>
            </view>

            <!-- 物料编号 -->
            <view class="filter-section">
              <view class="group-title">物料编号</view>
              <input
                class="input-field"
                placeholder="请输入物料编号"
                v-model="filterForm.materialCode"
              />
            </view>

            <!-- 物料名称 -->
            <view class="filter-section">
              <view class="group-title">物料名称</view>
              <input
                class="input-field"
                placeholder="请输入物料名称"
                v-model="filterForm.materialName"
              />
            </view>
          </view>

          <!-- 出库记录筛选条件 -->
          <view v-else>
            <!-- 出库单编号 -->
            <view class="filter-section">
              <view class="group-title">出库单编号</view>
              <input
                class="input-field"
                placeholder="请输入出库单编号"
                v-model="filterForm.outboundNo"
              />
            </view>

            <!-- 出库状态 -->
            <view class="filter-section">
              <view class="group-title">出库状态</view>
              <view class="checkbox-group">
                <view
                  v-for="status in outboundStatuses"
                  :key="status.value"
                  class="checkbox-item"
                  :class="{ checked: filterForm.outboundStatus.includes(status.value) }"
                  @click="toggleStatus(status.value, 'outbound')"
                >
                  <text class="checkbox-text">{{ status.label }}</text>
                </view>
              </view>
            </view>

            <!-- 出库类型 -->
            <view class="filter-section">
              <view class="group-title">出库类型</view>
              <view class="checkbox-group">
                <view
                  v-for="type in outboundTypes"
                  :key="type.value"
                  class="checkbox-item"
                  :class="{ checked: filterForm.outboundType.includes(type.value) }"
                  @click="toggleType(type.value, 'outbound')"
                >
                  <text class="checkbox-text">{{ type.label }}</text>
                </view>
              </view>
            </view>

            <!-- 物料编号 -->
            <view class="filter-section">
              <view class="group-title">物料编号</view>
              <input
                class="input-field"
                placeholder="请输入物料编号"
                v-model="filterForm.outMaterialCode"
              />
            </view>

            <!-- 物料名称 -->
            <view class="filter-section">
              <view class="group-title">物料名称</view>
              <input
                class="input-field"
                placeholder="请输入物料名称"
                v-model="filterForm.outMaterialName"
              />
            </view>
          </view>
        </scroll-view>

        <view class="filter-footer">
          <view class="filter-btn reset" @click="resetFilter">重置</view>
          <view class="filter-btn confirm" @click="applyFilter">确定</view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  name: "MaterialOperationRecords",
  data() {
    return {
      activeTab: "inbound", // 当前激活的标签页
      inboundRecords: [], // 入库记录列表
      outboundRecords: [], // 出库记录列表
      inboundLoading: false, // 入库加载状态
      outboundLoading: false, // 出库加载状态
      loadMoreStatus: "more", // 加载更多状态: more, loading, noMore
      inboundPage: {
        current: 1,
        size: 10,
        total: 0,
      },
      outboundPage: {
        current: 1,
        size: 10,
        total: 0,
      },
      expandedRecords: new Set(), // 展开的记录ID集合
      
      // 筛选相关
      showFilterModal: false,
      filterForm: {
        // 入库筛选条件
        inboundNo: "",
        inboundStatus: [],
        inboundType: [],
        planInboundStartTime: "",
        planInboundEndTime: "",
        actualInboundStartTime: "",
        actualInboundEndTime: "",
        warehouseName: "",
        locationName: "",
        materialCode: "",
        materialName: "",
        
        // 出库筛选条件
        outboundNo: "",
        outboundStatus: [],
        outboundType: [],
        planOutboundStartTime: "",
        planOutboundEndTime: "",
        actualOutboundStartTime: "",
        actualOutboundEndTime: "",
        outWarehouseName: "",
        outLocationName: "",
        outMaterialCode: "",
        outMaterialName: "",
      },
      
      // 入库状态选项
      inboundStatuses: [
        { label: "待入库", value: "1" },
        { label: "已入库", value: "2" },
        { label: "部分入库", value: "3" },
        { label: "已驳回", value: "4" },
      ],
      
      // 入库类型选项
      inboundTypes: [
        { label: "采购入库", value: "11" },
        { label: "生产入库", value: "12" },
        { label: "仓库调拨入库", value: "13" },
        { label: "其他入库", value: "19" },
      ],
      
      // 出库状态选项
      outboundStatuses: [
        { label: "待审核", value: "1" },
        { label: "已审核", value: "2" },
        { label: "已出库", value: "3" },
        { label: "已驳回", value: "4" },
        { label: "部分出库", value: "5" },
      ],
      
      // 出库类型选项
      outboundTypes: [
        { label: "销售出库", value: "1" },
        { label: "生产领料出库", value: "2" },
        { label: "仓库调拨出库", value: "3" },
        { label: "其他出库", value: "4" },
      ],
    };
  },
  onLoad() {
    this.loadInboundRecords();
  },
  onShow() {
    // 页面显示时刷新当前tab的数据
    if (this.activeTab === "inbound") {
      this.refreshInboundRecords();
    } else {
      this.refreshOutboundRecords();
    }
  },
  onReachBottom() {
    this.loadMore();
  },
  methods: {
    // 切换tab
    switchTab(tab) {
      if (this.activeTab === tab) return;

      this.activeTab = tab;
      if (tab === "inbound") {
        if (this.inboundRecords.length === 0) {
          this.loadInboundRecords();
        }
      } else {
        if (this.outboundRecords.length === 0) {
          this.loadOutboundRecords();
        }
      }
    },

    // 加载入库记录
    async loadInboundRecords(refresh = false) {
      if (this.inboundLoading) return;

      if (refresh) {
        this.inboundPage.current = 1;
        this.inboundRecords = [];
      }

      this.inboundLoading = true;

      try {
        // 构建筛选参数
        const params = {
          pageNo: this.inboundPage.current,
          pageSize: this.inboundPage.size,
        };

        // 添加筛选条件
        if (this.filterForm.inboundNo) {
          params.inboundNo = this.filterForm.inboundNo;
        }
        if (this.filterForm.inboundStatus.length > 0) {
          params.status = this.filterForm.inboundStatus.join(',');
        }
        if (this.filterForm.inboundType.length > 0) {
          params.businessType = this.filterForm.inboundType.join(',');
        }
        if (this.filterForm.planInboundStartTime) {
          params.planInboundStartTime = this.filterForm.planInboundStartTime;
        }
        if (this.filterForm.planInboundEndTime) {
          params.planInboundEndTime = this.filterForm.planInboundEndTime;
        }
        if (this.filterForm.actualInboundStartTime) {
          params.actualInboundStartTime = this.filterForm.actualInboundStartTime;
        }
        if (this.filterForm.actualInboundEndTime) {
          params.actualInboundEndTime = this.filterForm.actualInboundEndTime;
        }
        if (this.filterForm.warehouseName) {
          params.warehouseName = this.filterForm.warehouseName;
        }
        if (this.filterForm.locationName) {
          params.locationName = this.filterForm.locationName;
        }
        if (this.filterForm.materialCode) {
          params.materialCode = this.filterForm.materialCode;
        }
        if (this.filterForm.materialName) {
          params.materialName = this.filterForm.materialName;
        }

        const response = await this.$http.get(
          "/wms/inboundOrder/getUserOperationRecords",
          {
            params: params,
          }
        );

        if (response.data.success) {
          const records = response.data.result.records || [];
          if (refresh) {
            this.inboundRecords = records;
          } else {
            this.inboundRecords = [...this.inboundRecords, ...records];
          }

          this.inboundPage.total = response.data.result.total || 0;
          this.inboundPage.current++;

          // 更新加载更多状态
          if (this.inboundRecords.length >= this.inboundPage.total) {
            this.loadMoreStatus = "noMore";
          } else {
            this.loadMoreStatus = "more";
          }
        } else {
          uni.showToast({
            title: response.data.message || "加载失败",
            icon: "none",
          });
        }
      } catch (error) {
        console.error("加载入库记录失败:", error);
        uni.showToast({
          title: "网络错误",
          icon: "none",
        });
      } finally {
        this.inboundLoading = false;
      }
    },

    // 加载出库记录
    async loadOutboundRecords(refresh = false) {
      if (this.outboundLoading) return;

      if (refresh) {
        this.outboundPage.current = 1;
        this.outboundRecords = [];
      }

      this.outboundLoading = true;

      try {
        // 构建筛选参数
        const params = {
          pageNo: this.outboundPage.current,
          pageSize: this.outboundPage.size,
        };

        // 添加筛选条件
        if (this.filterForm.outboundNo) {
          params.outboundNo = this.filterForm.outboundNo;
        }
        if (this.filterForm.outboundStatus.length > 0) {
          params.status = this.filterForm.outboundStatus.join(',');
        }
        if (this.filterForm.outboundType.length > 0) {
          params.businessType = this.filterForm.outboundType.join(',');
        }
        if (this.filterForm.planOutboundStartTime) {
          params.planOutboundStartTime = this.filterForm.planOutboundStartTime;
        }
        if (this.filterForm.planOutboundEndTime) {
          params.planOutboundEndTime = this.filterForm.planOutboundEndTime;
        }
        if (this.filterForm.actualOutboundStartTime) {
          params.actualOutboundStartTime = this.filterForm.actualOutboundStartTime;
        }
        if (this.filterForm.actualOutboundEndTime) {
          params.actualOutboundEndTime = this.filterForm.actualOutboundEndTime;
        }
        if (this.filterForm.outWarehouseName) {
          params.warehouseName = this.filterForm.outWarehouseName;
        }
        if (this.filterForm.outLocationName) {
          params.locationName = this.filterForm.outLocationName;
        }
        if (this.filterForm.outMaterialCode) {
          params.materialCode = this.filterForm.outMaterialCode;
        }
        if (this.filterForm.outMaterialName) {
          params.materialName = this.filterForm.outMaterialName;
        }

        const response = await this.$http.get(
          "/wms/outbound/getUserOperationRecords",
          {
            params: params,
          }
        );

        if (response.data.success) {
          const records = response.data.result.records || [];
          if (refresh) {
            this.outboundRecords = records;
          } else {
            this.outboundRecords = [...this.outboundRecords, ...records];
          }

          this.outboundPage.total = response.data.result.total || 0;
          this.outboundPage.current++;

          // 更新加载更多状态
          if (this.outboundRecords.length >= this.outboundPage.total) {
            this.loadMoreStatus = "noMore";
          } else {
            this.loadMoreStatus = "more";
          }
        } else {
          uni.showToast({
            title: response.data.message || "加载失败",
            icon: "none",
          });
        }
      } catch (error) {
        console.error("加载出库记录失败:", error);
        uni.showToast({
          title: "网络错误",
          icon: "none",
        });
      } finally {
        this.outboundLoading = false;
      }
    },

    // 刷新入库记录
    refreshInboundRecords() {
      this.loadInboundRecords(true);
    },

    // 刷新出库记录
    refreshOutboundRecords() {
      this.loadOutboundRecords(true);
    },

    // 加载更多
    loadMore() {
      if (this.loadMoreStatus !== "more") return;

      if (this.activeTab === "inbound") {
        this.loadInboundRecords();
      } else {
        this.loadOutboundRecords();
      }
    },

    // 查看入库单详情
    viewInboundDetail(record) {
      uni.navigateTo({
        url: `/pages/wms/inbound/detail?id=${record.id}`,
      });
    },

    // 查看出库单详情
    viewOutboundDetail(record) {
      uni.navigateTo({
        url: `/pages/wms/outbound/detail?id=${record.id}`,
      });
    },

    // 格式化时间
    formatTime(time) {
      if (!time) return "";
      const date = new Date(time);
      const now = new Date();
      const diff = now.getTime() - date.getTime();

      if (diff < 60000) {
        // 1分钟内
        return "刚刚";
      } else if (diff < 3600000) {
        // 1小时内
        return Math.floor(diff / 60000) + "分钟前";
      } else if (diff < 86400000) {
        // 24小时内
        return Math.floor(diff / 3600000) + "小时前";
      } else {
        return (
          date.toLocaleDateString() +
          " " +
          date.toLocaleTimeString().slice(0, 5)
        );
      }
    },

    // 获取入库状态文本
    getInboundStatusText(status) {
      const statusMap = {
        1: "待入库",
        2: "已入库",
        3: "部分入库",
        4: "已驳回",
      };
      return statusMap[status] || "未知状态";
    },

    // 获取出库状态文本
    getOutboundStatusText(status) {
      const statusMap = {
        1: "待审核",
        2: "已审核",
        3: "已出库",
        4: "已驳回",
        5: "部分出库",
      };
      return statusMap[status] || "未知状态";
    },

    // 获取入库类型文本
    getInboundTypeText(record) {
      // 优先使用 businessTypeName，如果没有则使用 businessType_dictText
      if (record.businessTypeName) {
        return record.businessTypeName;
      }
      if (record.businessType_dictText) {
        return record.businessType_dictText;
      }
      // 兜底使用 businessType 映射
      const typeMap = {
        11: "采购入库",
        12: "生产入库",
        13: "仓库调拨入库",
        19: "其他入库",
      };
      return typeMap[record.businessType] || "未知类型";
    },

    // 获取出库类型文本
    getOutboundTypeText(type) {
      const typeMap = {
        1: "销售出库",
        2: "生产领料出库",
        3: "仓库调拨出库",
        4: "其他出库",
      };
      return typeMap[type] || "未知类型";
    },

    // 获取出库分配状态文本
    getOutboundAllocationStatusText(status) {
      const statusMap = {
        0: "未分配",
        1: "完全分配",
        2: "部分分配",
        3: "已分配",
      };
      return statusMap[status] || "未知状态";
    },

    // 获取分配方案状态文本
    getAllocationPlanStatusText(status) {
      const statusMap = {
        0: "待分配",
        1: "已出库",
        2: "已取消",
      };
      return statusMap[status] || "未知状态";
    },

    // 打开筛选弹窗
    openFilterModal() {
      this.showFilterModal = true;
    },

    // 切换状态筛选
    toggleStatus(statusValue, type = 'inbound') {
      const field = type === 'inbound' ? 'inboundStatus' : 'outboundStatus';
      const index = this.filterForm[field].indexOf(statusValue);
      if (index > -1) {
        this.filterForm[field].splice(index, 1);
      } else {
        this.filterForm[field].push(statusValue);
      }
    },

    // 切换类型筛选
    toggleType(typeValue, type = 'inbound') {
      const field = type === 'inbound' ? 'inboundType' : 'outboundType';
      const index = this.filterForm[field].indexOf(typeValue);
      if (index > -1) {
        this.filterForm[field].splice(index, 1);
      } else {
        this.filterForm[field].push(typeValue);
      }
    },

    // 重置筛选
    resetFilter() {
      this.filterForm = {
        // 入库筛选条件
        inboundNo: "",
        inboundStatus: [],
        inboundType: [],
        planInboundStartTime: "",
        planInboundEndTime: "",
        actualInboundStartTime: "",
        actualInboundEndTime: "",
        warehouseName: "",
        locationName: "",
        materialCode: "",
        materialName: "",
        
        // 出库筛选条件
        outboundNo: "",
        outboundStatus: [],
        outboundType: [],
        planOutboundStartTime: "",
        planOutboundEndTime: "",
        actualOutboundStartTime: "",
        actualOutboundEndTime: "",
        outWarehouseName: "",
        outLocationName: "",
        outMaterialCode: "",
        outMaterialName: "",
      };
    },

    // 应用筛选
    applyFilter() {
      this.showFilterModal = false;
      if (this.activeTab === "inbound") {
        this.refreshInboundRecords();
      } else {
        this.refreshOutboundRecords();
      }
    },

    // 切换记录展开状态
    toggleRecordExpand(recordId) {
      if (this.expandedRecords.has(recordId)) {
        this.expandedRecords.delete(recordId);
      } else {
        this.expandedRecords.add(recordId);
      }
      // 触发响应式更新
      this.$forceUpdate();
    },

    // 检查记录是否展开
    isRecordExpanded(recordId) {
      return this.expandedRecords.has(recordId);
    },

    // 检查是否有激活的筛选条件
    hasActiveFilters() {
      if (this.activeTab === 'inbound') {
        return (
          this.filterForm.inboundNo ||
          this.filterForm.inboundStatus.length > 0 ||
          this.filterForm.inboundType.length > 0 ||
          this.filterForm.planInboundStartTime ||
          this.filterForm.planInboundEndTime ||
          this.filterForm.actualInboundStartTime ||
          this.filterForm.actualInboundEndTime ||
          this.filterForm.warehouseName ||
          this.filterForm.locationName ||
          this.filterForm.materialCode ||
          this.filterForm.materialName
        );
      } else {
        return (
          this.filterForm.outboundNo ||
          this.filterForm.outboundStatus.length > 0 ||
          this.filterForm.outboundType.length > 0 ||
          this.filterForm.planOutboundStartTime ||
          this.filterForm.planOutboundEndTime ||
          this.filterForm.actualOutboundStartTime ||
          this.filterForm.actualOutboundEndTime ||
          this.filterForm.outWarehouseName ||
          this.filterForm.outLocationName ||
          this.filterForm.outMaterialCode ||
          this.filterForm.outMaterialName
        );
      }
    },

    // 获取类型样式类（统一处理入库和出库）
    getTypeClass(type, category) {
      if (category === "inbound") {
        const inboundTypeMap = {
          11: "purchase-type", // 采购入库
          12: "production-type", // 生产入库
          13: "transfer-type", // 仓库调拨入库
          19: "other-type", // 其他入库
        };
        return inboundTypeMap[type] || "default-type";
      } else if (category === "outbound") {
        const outboundTypeMap = {
          1: "sales-type", // 销售出库
          2: "material-type", // 生产领料出库
          3: "transfer-type", // 仓库调拨出库
          4: "other-type", // 其他出库
          21: "material-type", // 生产领料出库
          22: "transfer-type", // 仓库调拨出库
          29: "other-type", // 其他出库
        };
        return outboundTypeMap[type] || "default-type";
      }
      return "default-type";
    },
  },
};
</script>

<style lang="scss" scoped>
.operation-records {
  background: #f5f7fa;
  min-height: 100vh;
}

.nav-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 24rpx;
  text-align: center;

  .nav-title {
    font-size: 36rpx;
    font-weight: bold;
  }
}

.tab-container {
  background: white;
  padding: 0 24rpx;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
}

.tab-list {
  display: flex;
  align-items: center;
}

.tab-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 32rpx 0;
  position: relative;
  transition: all 0.3s ease;

  &.active {
    .tab-icon {
      background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
      transform: scale(1.1);
    }

    .tab-text {
      color: #1890ff;
      font-weight: bold;
    }

    &::after {
      content: "";
      position: absolute;
      bottom: 0;
      left: 50%;
      transform: translateX(-50%);
      width: 60rpx;
      height: 6rpx;
      background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%);
      border-radius: 3rpx;
    }
  }

  .tab-icon {
    width: 80rpx;
    height: 80rpx;
    background: #f0f2f5;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 16rpx;
    transition: all 0.3s ease;

    .iconfont {
      font-size: 36rpx;
      color: white;
    }
  }

  .tab-text {
    font-size: 28rpx;
    color: #666;
    transition: all 0.3s ease;
  }
}

.records-container {
  padding: 24rpx;
}

.record-list {
  .record-item {
    background: white;
    border-radius: 20rpx;
    margin-bottom: 24rpx;
    padding: 32rpx;
    box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.06);
    border: 2rpx solid transparent;
    transition: all 0.3s ease;
    position: relative;
    overflow: hidden;

    &::before {
      content: "";
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      height: 6rpx;
    }

    &.inbound-item::before {
      background: linear-gradient(90deg, #52c41a 0%, #389e0d 100%);
    }

    &.outbound-item::before {
      background: linear-gradient(90deg, #fa8c16 0%, #d46b08 100%);
    }

    &:active {
      transform: scale(0.98);
      box-shadow: 0 12rpx 32rpx rgba(0, 0, 0, 0.12);
    }
  }
}

.record-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 24rpx;

  .record-title {
    flex: 1;

    .order-no {
      font-size: 32rpx;
      font-weight: bold;
      color: #262626;
      margin-bottom: 8rpx;
    }

    .record-status {
      display: inline-block;
      padding: 8rpx 16rpx;
      border-radius: 20rpx;
      font-size: 22rpx;
      font-weight: 500;

      // 入库单状态样式
      &.status-0 {
        background: #fff7e6;
        color: #fa8c16;
        border: 1rpx solid #ffd591;
      }

      &.status-1 {
        background: #f6ffed;
        color: #52c41a;
        border: 1rpx solid #b7eb8f;
      }

      &.status-2 {
        background: #e6f7ff;
        color: #1890ff;
        border: 1rpx solid #91d5ff;
      }

      &.status-3 {
        background: #f6ffed;
        color: #52c41a;
        border: 1rpx solid #b7eb8f;
      }

      &.status-4 {
        background: #fff2f0;
        color: #ff4d4f;
        border: 1rpx solid #ffccc7;
      }

      // 出库单状态样式 - status-5 部分出库
      &.status-5 {
        background: #e6f7ff;
        color: #1890ff;
        border: 1rpx solid #91d5ff;
      }
    }
  }

  .record-time {
    font-size: 24rpx;
    color: #8c8c8c;
    white-space: nowrap;
  }
}

.record-content {
  .record-info {
    margin-bottom: 20rpx;

    .info-item {
      display: flex;
      align-items: center;
      margin-bottom: 12rpx;

      &:last-child {
        margin-bottom: 0;
      }

      .info-label {
        font-size: 26rpx;
        color: #8c8c8c;
        margin-right: 16rpx;
        min-width: 120rpx;
      }

      .info-value {
        font-size: 26rpx;
        color: #262626;
        flex: 1;
      }
    }
  }

  .record-details {
    background: #fafbfc;
    border-radius: 12rpx;
    padding: 20rpx;

    .detail-summary {
      font-size: 24rpx;
      color: #8c8c8c;
      margin-bottom: 16rpx;
    }

    .detail-list {
      .detail-item {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 8rpx;

        &:last-child {
          margin-bottom: 0;
        }

        .material-name {
          font-size: 26rpx;
          color: #262626;
          flex: 1;
          margin-right: 16rpx;
        }

        .material-quantity {
          font-size: 26rpx;
          color: #1890ff;
          font-weight: 500;
        }
      }

      .more-details {
        font-size: 24rpx;
        color: #8c8c8c;
        text-align: center;
        margin-top: 12rpx;
        padding-top: 12rpx;
        border-top: 1rpx solid #f0f0f0;
      }
    }
  }
}

.loading-container,
.empty-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 120rpx 0;
}

.empty-container {
  .empty-icon {
    width: 120rpx;
    height: 120rpx;
    background: #f5f5f5;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 24rpx;

    .iconfont {
      font-size: 48rpx;
      color: #bfbfbf;
    }
  }

  .empty-text {
    font-size: 28rpx;
    color: #8c8c8c;
  }
}

.load-more-container {
  padding: 24rpx;
}

// 用户操作详情面板样式
.user-operations-panel {
  margin-top: 20rpx;
  background: #ffffff;
  border-radius: 16rpx;
  padding: 24rpx;
  border: 1rpx solid #e9ecef;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);

  .panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20rpx;
    padding-bottom: 16rpx;
    border-bottom: 1rpx solid #f0f0f0;

    .panel-title {
      font-size: 30rpx;
      font-weight: 600;
      color: #2c3e50;
    }

    // 入库类型标签样式
    .inbound-type-tag,
    .outbound-type-tag {
      padding: 6rpx 12rpx;
      border-radius: 20rpx;
      font-size: 22rpx;
      font-weight: 500;

      .type-text {
        color: white;
      }

      // 采购入库
      &.purchase-type {
        background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
      }

      // 生产入库
      &.production-type {
        background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
      }

      // 调拨入库/出库
      &.transfer-type {
        background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
      }

      // 销售出库
      &.sales-type {
        background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%);
      }

      // 生产领料出库
      &.material-type {
        background: linear-gradient(135deg, #43e97b 0%, #38f9d7 100%);
      }

      // 其他入库/出库
      &.other-type {
        background: linear-gradient(135deg, #fa709a 0%, #fee140 100%);
      }

      // 默认类型
      &.default-type {
        background: linear-gradient(135deg, #a8edea 0%, #fed6e3 100%);
      }
    }
  }

  .operations-list {
    .operation-item {
      background: #fafbfc;
      border-radius: 12rpx;
      padding: 20rpx;
      margin-bottom: 16rpx;
      border: 1rpx solid #f0f0f0;
      transition: all 0.3s ease;

      &:last-child {
        margin-bottom: 0;
      }

      &:active {
        background: #f5f6fa;
        transform: scale(0.98);
      }

      .item-header {
        display: flex;
        justify-content: space-between;
        align-items: flex-start;
        margin-bottom: 16rpx;

        .material-name {
          font-size: 30rpx;
          color: #2c3e50;
          font-weight: 600;
          flex: 1;
          margin-right: 16rpx;
          line-height: 1.4;
        }

        .operation-time {
          font-size: 24rpx;
          color: #7f8c8d;
          white-space: nowrap;
        }
      }

      .item-content {
        display: flex;
        justify-content: space-between;
        margin-bottom: 16rpx;

        .material-details {
          flex: 1;
          margin-right: 20rpx;

          .detail-row {
            display: flex;
            align-items: center;
            margin-bottom: 8rpx;

            &:last-child {
              margin-bottom: 0;
            }

            .label {
              font-size: 24rpx;
              color: #95a5a6;
              min-width: 60rpx;
              margin-right: 12rpx;
            }

            .value {
              font-size: 26rpx;
              color: #34495e;
              flex: 1;
            }
          }
        }

        .quantity-section {
          display: flex;
          flex-direction: column;
          min-width: 120rpx;

          .quantity-item {
            display: flex;
            align-items: center;
            justify-content: flex-end;
            margin-bottom: 8rpx;
            padding: 6rpx 12rpx;
            border-radius: 8rpx;

            &:last-child {
              margin-bottom: 0;
            }

            &.plan {
              background: #e8f4fd;
            }

            &.actual {
              background: #e8f5e8;
            }

            &.allocated {
              background: #fff3cd;
            }

            &.remaining {
              background: #f8d7da;
            }

            .qty-label {
              font-size: 22rpx;
              color: #7f8c8d;
              margin-right: 8rpx;
            }

            .qty-value {
              font-size: 26rpx;
              font-weight: 600;
              margin-right: 4rpx;

              .plan & {
                color: #3498db;
              }

              .actual & {
                color: #27ae60;
              }

              .allocated & {
                color: #856404;
              }

              .remaining & {
                color: #721c24;
              }
            }

            .qty-unit {
              font-size: 22rpx;
              color: #95a5a6;
            }
          }
        }
      }

      .item-footer {
        border-top: 1rpx solid #ecf0f1;
        padding-top: 12rpx;

        .location-row,
        .remark-row {
          display: flex;
          align-items: flex-start;
          margin-bottom: 8rpx;

          &:last-child {
            margin-bottom: 0;
          }
        }

        .location-icon,
        .remark-icon {
          font-size: 24rpx;
          margin-right: 8rpx;
          margin-top: 2rpx;
        }

        .location-text,
        .remark-text {
          font-size: 24rpx;
          color: #7f8c8d;
          flex: 1;
          line-height: 1.4;
        }

        .remark-text {
          font-style: italic;
        }
      }
    }

    .no-operations {
      text-align: center;
      padding: 32rpx 0;
      color: #6c757d;
      font-size: 26rpx;
    }
  }
}

// 展开按钮样式
.expand-button {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 16rpx;
  padding: 12rpx 0;
  background: #f8f9fa;
  border-radius: 8rpx;
  border: 1rpx solid #e9ecef;
  transition: all 0.3s ease;

  &:active {
    background: #e9ecef;
  }

  .expand-text {
    font-size: 24rpx;
    color: #6c757d;
    margin-right: 8rpx;
  }

  .expand-icon {
    font-size: 20rpx;
    color: #6c757d;
    transition: transform 0.3s ease;

    &.expanded {
      transform: rotate(180deg);
    }
  }
}

// 自定义导航栏右侧按钮样式
.nav-right {
  display: flex;
  align-items: center;
  padding: 8rpx 16rpx;
  border-radius: 20rpx;
  background: rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;

  &:active {
    background: rgba(255, 255, 255, 0.3);
  }

  .cuIcon-filter {
    font-size: 28rpx;
    margin-right: 6rpx;
  }

  .filter-text {
    font-size: 24rpx;
  }
}

.nav-right {
  display: flex;
  align-items: center;
  color: white;
  font-size: 28rpx;
  position: relative;

  .cuIcon-filter {
    margin-right: 8rpx;
    font-size: 32rpx;
    transition: all 0.3s ease;
    
    &.filter-active {
      color: #ffd700;
      transform: scale(1.1);
    }
  }

  .filter-text {
    font-size: 28rpx;
    transition: all 0.3s ease;
    
    &.filter-active {
      color: #ffd700;
      font-weight: bold;
    }
  }
  
  .filter-dot {
    position: absolute;
    top: -4rpx;
    right: -4rpx;
    width: 16rpx;
    height: 16rpx;
    background: #ff4d4f;
    border-radius: 50%;
    border: 2rpx solid white;
  }
}

// 筛选弹窗样式
.filter-modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  z-index: 9999;
  display: flex;
  align-items: flex-end;

  .filter-content {
    width: 100%;
    max-height: 80vh;
    background: #ffffff;
    border-radius: 20rpx 20rpx 0 0;
    display: flex;
    flex-direction: column;

    .filter-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 32rpx;
      border-bottom: 1rpx solid #f0f0f0;

      .filter-title {
        font-size: 32rpx;
        font-weight: 600;
        color: #333333;
      }

      .close-btn {
        width: 60rpx;
        height: 60rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        color: #999999;
        font-size: 32rpx;
      }
    }

    .filter-body {
      flex: 1;
      padding: 0 32rpx;
      max-height: 60vh;

      .filter-section {
        margin-bottom: 40rpx;

        .group-title {
          font-size: 28rpx;
          font-weight: 600;
          color: #333333;
          margin-bottom: 20rpx;
        }

        .input-field {
          width: 100%;
          height: 80rpx;
          padding: 0 24rpx;
          border: 1rpx solid #e0e0e0;
          border-radius: 12rpx;
          font-size: 28rpx;
          color: #333333;
          background: #ffffff;

          &:focus {
            border-color: #007aff;
          }
        }

        .checkbox-group {
          display: flex;
          flex-wrap: wrap;
          gap: 20rpx;

          .checkbox-item {
            padding: 16rpx 24rpx;
            border: 1rpx solid #e0e0e0;
            border-radius: 40rpx;
            background: #ffffff;
            transition: all 0.3s ease;

            &.checked {
              background: #007aff;
              border-color: #007aff;

              .checkbox-text {
                color: #ffffff;
              }
            }

            .checkbox-text {
              font-size: 26rpx;
              color: #666666;
              transition: color 0.3s ease;
            }
          }
        }
      }
    }

    .filter-footer {
      display: flex;
      padding: 32rpx;
      gap: 24rpx;
      border-top: 1rpx solid #f0f0f0;

      .filter-btn {
        flex: 1;
        height: 88rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 12rpx;
        font-size: 30rpx;
        font-weight: 600;
        transition: all 0.3s ease;

        &.reset {
          background: #f5f5f5;
          color: #666666;

          &:active {
            background: #e0e0e0;
          }
        }

        &.confirm {
          background: #007aff;
          color: #ffffff;

          &:active {
            background: #0056cc;
          }
        }
      }
    }
  }
}
</style>
